/******************************************************************************
 * Copyright (C) ShenZhen Powerdata Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳市博安达信息技术股份有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 *****************************************************************************/

package com.szboanda.business.workflow.controller;

import com.szboanda.beijing.pure.BaseBusinessController;
import com.szboanda.business.workflow.exception.WorkflowException;
import com.szboanda.business.workflow.service.IWorkflowService;
import com.szboanda.platform.common.utils.MapUtils;
import com.szboanda.platform.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title: 流程处理意见_控制器
 * @fileName: WorkflowController.java
 * @description:
 * @copyright: PowerData Software Co.,Ltd. Rights Reserved.
 * @company: 深圳市博安达信息技术股份有限公司
 * @author: 原晋川
 * @date: 2019-08-23 15:37
 * @version: V1.0
 */
@Controller
@RequestMapping("/business/workflow/workflowcontroller")
public class WorkflowController extends BaseBusinessController {

    /**
     * 注入处理业务接口
     */
    @Autowired
    private IWorkflowService workflowService;


    /**
     * 跳转到【流程处理意见】查询页面
     *
     * @param model 页面View的Model对象
     * @return
     * @throws WorkflowException
     */
    @RequestMapping("/list")
    public String listPage(ModelMap model) throws WorkflowException {
        this.setToken();
        return "/business/workflow/workflowlist";
    }

    /**
     * 跳转到【流程处理意见】编辑页面
     *
     * @param model 页面View的Model对象
     * @return
     * @throws WorkflowException
     */
    @RequestMapping("/edit")
    public String editPage(ModelMap model) throws WorkflowException {
        this.setToken();
        HttpServletRequest request = this.getRequest();
        model.addAttribute("readonly", request.getParameter("readonly"));
        model.addAttribute("BH", request.getParameter("BH"));
        return "/business/workflow/workflowedit";

    }

    /**
     * 查询【流程处理意见】业务集合信息
     *
     * @param params 业务集合
     * @return 返回Map集合信息
     * @throws WorkflowException
     */
    @RequestMapping(value = "/queryworkflows", method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> queryWorkflows(@RequestBody Map<String, Object> params) throws WorkflowException {
        Map<String, Object> result = workflowService.queryWorkflows(params);
        String copyReaders = workflowService.queryCopyReaders(params);
        result.put("FYRY", StringUtils.isEmpty(copyReaders) ? "" : copyReaders);
        return result;
    }

    /**
     * 根据流程号查询当前步骤是否为流程发起人
     * 只有刚发起未流转的件允许删除
     *
     * @param params
     * @return
     * @throws WorkflowException
     */
    @RequestMapping(value = "/checkCurrentStepIsFirstStep", method = {RequestMethod.POST})
    @ResponseBody
    public String checkCurrentStepIsFirstStep(@RequestBody Map<String, Object> params) throws WorkflowException {
        String[] ids = MapUtils.getString(params, "instanceids").split(",");
        //定义个返回值，true可以删除，false不可删除
        String result = "true";

        for (int i = 0; i < ids.length; ++i) {
            Map<String, Object> currentWorkFlowMap = workflowService.queryCurrentStepByInstanceid(ids[i]);
            if (!StringUtils.isEmpty(currentWorkFlowMap)) {
                Map<String, Object> firstWorkFlowMap = workflowService.queryFirstStepByInstanceid((String) currentWorkFlowMap.get("BZDYBH"));
                if (!StringUtils.isEmpty(firstWorkFlowMap)) {
                    if (!"1".equals(firstWorkFlowMap.get("PXH"))) {
                        result = "false";
                        break;
                    }
                } else {
                    result = "false";
                    break;
                }
            }
        }
        return result;
    }

    /**
     * 判断用户是否关注该流程
     *
     * @param params {YHID:"", LCSLBH:""}
     * @return
     */
    @RequestMapping(value = "/isAttention", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> isAttention(@RequestBody Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        result.put("result", "未知错误");
        if (params != null) {
            Object yhid = params.get("YHID");
            Object gzlcslbh = params.get("LCSLBH");
            if (StringUtils.isNotEmpty(yhid) && StringUtils.isNotEmpty(gzlcslbh)) {
                boolean isAttention = workflowService.isAttention(yhid.toString(), gzlcslbh.toString());
                if (isAttention) {
                    result.put("result", "已关注");
                } else {
                    result.put("result", "未关注");
                }
            }
        }
        return result;
    }

    /**
     * 设置关注
     *
     * @param params {YHID:"", LCSLBH:"", CZLX:""}
     *               其中CZLX的值为SZGZ（设置关注）和QXGZ（取消关注）
     * @return
     */
    @RequestMapping(value = "/setAttention", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> setAttention(@RequestBody Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        result.put("result", "参数为空");
        if (params != null) {
            String yhid = StringUtils.isEmpty(params.get("YHID")) ? null : params.get("YHID").toString();
            String lcslbh = StringUtils.isEmpty(params.get("LCSLBH")) ? null : params.get("LCSLBH").toString();
            String czlx = StringUtils.isEmpty(params.get("CZLX")) ? null : params.get("CZLX").toString();

            if (StringUtils.isNotEmpty(yhid) && StringUtils.isNotEmpty(lcslbh)) {
                boolean isAttention = workflowService.isAttention(yhid, lcslbh);
                if ("SZGZ".equals(czlx)) {
                    if (isAttention) {
                        result.put("result", "已关注");
                    } else {
                        workflowService.setAttention(yhid, lcslbh);
                        result.put("result", "成功");
                    }
                } else if ("QXGZ".equals(czlx)) {
                    if (!isAttention) {
                        result.put("result", "未关注");
                    } else {
                        workflowService.cancelAttention(yhid, lcslbh);
                        result.put("result", "成功");
                    }
                }
            }

        }
        return result;
    }

    /**
     * 获取业务流程类型Map集合
     *
     * @return
     */
    @RequestMapping(value = "/getWorkflowTypeMap", method = RequestMethod.POST)
    @ResponseBody
    public List<Map<String, Object>> getWorkflowTypeMap() {
        return workflowService.queryWorkflowTypeMap();
    }
}